package org.webrtc.yl;

import android.app.Application;
import android.content.Intent;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.camera.base.WebSocketEventHandler;
import com.camera.base.inerface.CameraServiceSingleton;
import com.camera.base.inerface.ICPInterface;
import com.camera.base.inerface.WebModelFactory;
import com.camera.base.inerface.YLWebrtcInterface;
import com.camera.base.util.DefaultWebModelFactory;
import com.camera.base.wbmodels.WebModel;
import com.camera.routemodule.routeinterface.RouterUtil;
import com.camera.routemodule.routeinterface.YLWebSocketInterface;
import com.google.auto.service.AutoService;

import org.webrtc.SurfaceViewRenderer;
import org.webrtc.yl.WebSocket.AndroidDirectRTC;
import org.webrtc.yl.rtc.ProxyVideoSink;
import org.webrtc.yl.rtc.RTCEngine;
import org.webrtc.yl.ui.ConnectActivity;
import org.webrtc.yl.ui.ConnectManager;
import org.webrtc.yl.utils.StatsReportUtil;

@AutoService(YLWebrtcInterface.class)
public class YLWebrtcManager implements YLWebrtcInterface {
    private final String TAG = "YLWebrtcManager";
    private static YLWebrtcManager INSTANCE;
    final static private boolean DEBUG = false;
    private Application application;
    private WebModelFactory mWebModelFactory  = new DefaultWebModelFactory();
    public YLWebSocketInterface mWebSocket;
    private ICPInterface mICPInterface;
    private RTCEngine mRtcEngine;
    private boolean isSwappedFeeds;
    private StatsReportUtil statsReportUtil;

    private long callStartedTimeMs;

    private boolean haveVideo = true;

    public static YLWebrtcManager getInstance() {
        if (INSTANCE == null){
            INSTANCE = new YLWebrtcManager();
        }
        return INSTANCE;
    }

    @Override
    public void init(@Nullable Application application) {
        this.application = application;

        mWebSocket = RouterUtil.INSTANCE.getWebSocketImpl();
        if (mWebSocket != null){
            mWebSocket.addEventHandler(ylEventHandler);
        }
    }

    @Override
    public void sendMessage(@Nullable String message) {
        if (mWebSocket != null) {
            mWebSocket.sendMessage(message);//返回用户列表 以及 聊天室列表
        }
    }

    @Override
    public void addEventHandler(@NonNull WebSocketEventHandler event) {
    }

    @Override
    public void removeEventHandler(@NonNull WebSocketEventHandler event) {
    }


    WebSocketEventHandler ylEventHandler = new WebSocketEventHandler() {
        @Override
        public void onWebSocketMeesage(@NonNull WebModel mWebModel) {
            if (mWebModel.getFoctory().equals("webrtc")){
                if (mWebModel.getState().equals("onopen")){
                    startWebrtc(mWebModel.getWebrtcWebModel().getType());
                }
            }
        }
    };


    ////////
    private void startWebrtc(String webrtcType){
        if (DEBUG) Log.w(TAG, "startWebrtc ----------------");
        if (false){//是否需要界面
            Intent intent = new Intent(application, ConnectActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.putExtra("webrtc", webrtcType);
            application.startActivity(intent);
        }else{
            ConnectManager aa = new ConnectManager();
            aa.init(webrtcType, application);
        }
    }
}
